<template>
  <div class="dashboard-container body-min-width">
    <h2 class="head-big-title">{{detailData.categoryName?detailData.categoryName:'Produk A'}}</h2>
    <Pathnavigation style="margin-left: 3%;"></Pathnavigation>
    <div style="width: 92%;margin: 0 4% 30px 4%;background-color: white;text-align: left;padding: 10px 1% 50px 2%;">
      <el-form
        :inline="true"
        :model="detailData"
        label-position="top"
        size="medium"
        label-width="110px">
        <el-form-item label="Nama Kategori: " class="one-hang">
          <el-input v-model="detailData.categoryName" @input="changeNew($event)" placeholder="Nama" maxlength="50"></el-input>
        </el-form-item>
        <el-form-item label="Aturan Susunan: " class="one-hang">
          <el-input v-model="detailData.index" @input="changeNew($event)" placeholder="Aturan Susunan" type="number"></el-input>
        </el-form-item>
        <el-form-item label="Status Produk: ">
          <el-switch
            v-model="detailData.state"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="0"
            :inactive-value="1">
          </el-switch>
        </el-form-item>
        <div>
          <el-button type="primary" @click="saveDataNow" style="float: right;margin-left: 100px;">Simpan</el-button>
          <el-button style="float: right;" @click="$router.push({ name: 'Category' })">Balik</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { editCategoryApi, editDealer_productApi } from '@/api/user'

export default {
  name: 'CustomersDetails',
  computed: {
    ...mapGetters([
      'name',
      'role'
    ])
  },
  data() {
    return {
      detailData: {},
      orderChart: '',
      willDel: [],
      imageUrl: ''
    }
  },
  mounted() {
    this.checkID()
  },
  methods: {
    changeNew() {
      this.$forceUpdate()
    },
    checkID() {
      const detail = this.$getDetail()
      if (detail) {
        if (detail.categoryName && detail.categoryId) {
          this.detailData = detail
          return true
        }
      }
      this.$router.push({ name: 'Category' })
    },
    saveDataNow() {
      localStorage.removeItem('categoryList')
      if (this.role == this.dealerMValue) {
        editDealer_productApi(this.detailData).then(res => {
          this.$message({
            message: 'Berjaya ubah!',
            type: 'success'
          })
          this.detailData = { }
          this.$router.push({ name: 'Category' })
        }).catch(err => {
          console.log(err)
        })
      } else {
        editCategoryApi(this.detailData).then(res => {
          this.$message({
            message: 'Berjaya ubah!',
            type: 'success'
          })
          this.detailData = { }
          this.$router.push({ name: 'Category' })
        }).catch(err => {
          console.log(err)
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  .head-big-title{
    text-align: left;
    font-size: 20px;
    margin: 20px 0 0 3%;
  }
  .dashboard-container{
    margin-top: 20px;
    text-align: center;
  }

</style>
<style scoped>
  /deep/.head-search label{
    line-height: 30px;
  }
  .one-hang{
    display: block;
  }
  .big-title{
    text-align: left;
    font-size: 19px;
    margin: 20px 0;
  }
</style>
